<template>
	<view class="">
		<div v-if="gjp">
			<u-skeleton :loading="true" :animate="true" rows="7"  rowsHeight="100"></u-skeleton>
		</div>
		<div class="resultBox" v-else>
			<div class="container" v-if="zwDetail.length > 0">
				<div class="items">
					<div class="item" v-for="(item,index) in zwDetail" :key="index" v-if="item" @click="showDetail(item.year,item.wyz)">
						<h3>{{ item.zwmc }}</h3>
						<p><span>年份</span> {{ item.year }}</p>
						<p><span>公告来源</span> {{ item.ggxx }}</p>
						<p><span>工作单位</span> {{ item.bmmc }}</p>
						<div class="btnBox">
							<u-button type="success" shape="circle" size="mini" text="收藏职位+"
								@click.native.stop="addFav(item.year,item.wyz)" v-if="!favzw.includes(item.year + '-' + item.wyz)"></u-button>
							<u-button type="success" shape="circle" size="mini" text="取消收藏-"
								@click.native.stop="removeFav(item.year,item.wyz)" v-else :plain="true"></u-button>
						</div>
					</div>
				</div>
			</div>
		</div>
		<div >
			<u-gap height="60" bgColor="#fff"></u-gap>
		</div>
		<div class="fixBottom">
			<u-button type="success" text="职位对比" size="small" icon="eye" @click="compare()"></u-button>
		</div>
		
		<u-toast ref="uToast"></u-toast>
	</view>
</template>

<script>
	import {
		mapActions
	} from 'vuex';
	export default {
		data() {
			return {
				gjp: true,
				year: "2022",
				wyz: "",
				zwLink: "https://hlj.offcn.com/zg/lynn/sk-tools/zwb2022.js",
				linkList: [
					{year: "2023", link: "https://hlj.offcn.com/zg/lynn/sk-tools/zwb2023.js"},
					{year: "2022", link: "https://hlj.offcn.com/zg/lynn/sk-tools/zwb2022.js"},
					{year: "2021", link: "https://hlj.offcn.com/zg/lynn/sk-tools/zwb2022.js"}
				],
				zwDetail: []
			};
		},
		onShow() {},
		onPullDownRefresh() {
			console.log('refresh');
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onPageScroll(e) {
			let that = this;
		},
		onLoad(options) {
			if (!options) {} else {
				
			}
		},
		computed: {
			favzw() {
				return this.$store.getters.favzw ? this.$store.getters.favzw : '';
			}
		},
		mounted() {
			this.getFavzw()
		},
		watch: {},
		created() {
			let that = this;
			
		},
		methods: {
			...mapActions(['LogSetSearchtimes','LogSetPhone','LogSetOpenid','LogSetName','LogSetPage','LogSetFavzw']),
			getFavzw(){
				let that = this
				let fZw = that.favzw
				
				fZw.map((item,index) => {
					let s = item.split('-')
					let y = s[0]
					let w = s[1]
					let l = that.linkList.filter(q => q.year == y)[0].link
					that.getZwDetail(l,w,index,y)
				})
				
			},
			getZwDetail(link,w,index,y){
				let that = this
				uni.request({
					url: link,
					method: 'get',
					dataType: 'text',
					data: {
						sstimes: new Date()
					},
					success: function(res) {
						var resjson = JSON.parse(res.data);
						that.filterZw(resjson,w,index,y)
					}
				});
			},
			filterZw(zw,w,index,y){
				let that = this
				let fzw = zw.filter(item => item.wyz == w)
				let z = fzw[0]
				z.year = y
				z.ggxx = y + '年黑龙江公务员考试'
				// that.$set(that.zwDetail,index,z)
				that.zwDetail.push(z)
				if(index == that.favzw.length - 1){
					that.gjp = false
				}
			},
			showToast(type, info) {
				this.$refs.uToast.show({
					type: type,
					icon: false,
					title: '失败主题',
					message: info,
				})
			},
			imgView(imgUrl) {
				wx.previewImage({
					urls: [imgUrl], //需要预览的图片http链接列表，注意是数组
					current: '', // 当前显示图片的http链接，默认是第一个
					success: function(res) {},
					fail: function(res) {},
					complete: function(res) {}
				});
			},
			addFav(year,wyz) {
				let that = this
				let zwinfo = year + '-' + wyz
				let allFav = that.favzw
				if (!allFav.includes(zwinfo)) {
					allFav.push(zwinfo)
					that.LogSetFavzw(allFav)
					console.log(that.favzw)
				}
			},
			removeFav(year,wyz) {
				let that = this
				let zwinfo = year + '-' + wyz
				let allFav = that.favzw
				if (allFav.includes(zwinfo)) {
					allFav = allFav.filter(item => item != zwinfo)
					that.LogSetFavzw(allFav)
				}
			},
			compare(){
				uni.navigateTo({
					url: './compare'
				})
			},
			showDetail(year,wyz){
				uni.navigateTo({
					url: '../zwxq/index?year=' + year + '&wyz=' + wyz
				})
			},
		},
		components: {}
	};
</script>

<style lang="scss">
	@import url(./index.css);
</style>
